package com.lxb.myblog.controller.admin;

import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxb.myblog.po.Blog;
import com.lxb.myblog.po.Tag;
import com.lxb.myblog.po.User;
import com.lxb.myblog.queryvo.BlogQuery;
import com.lxb.myblog.service.BlogService;
import com.lxb.myblog.service.TagService;
import com.lxb.myblog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    // 博客列表
    @RequestMapping("/blogs")
    public String blogs(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        String orderBy = "update_time desc";
        PageHelper.startPage(pageNum, 10, orderBy);
        List<BlogQuery> list = blogService.getAllBlog();
        PageInfo<BlogQuery> pageInfo = new PageInfo<BlogQuery>(list);
        model.addAttribute("types", typeService.getAllType());
        model.addAttribute("pageInfo", pageInfo);
        return "admin/blogs";
    }

    @GetMapping("/input")
    public String input(Model model) {
        model.addAttribute("types", typeService.getAllType());
        model.addAttribute("tags", tagService.getAllTag());
        model.addAttribute("blog", new Blog());
        return "admin/blog-input";
    }

    @PostMapping("/blogs")
    public String input(Blog blog, HttpSession session, RedirectAttributes attributes) {
        User user = (User) session.getAttribute("user");
        blog.setUser(user);
        blog.setUserId(user.getId());
        blog.setTypeId(blog.getType().getId());
        blog.setTags(tagService.listTag(blog.getTagIds()));

        int result;
        if (blog.getId() == null) {
            result = blogService.saveBlog(blog);
            for(Tag tag : blog.getTags()){
                blogService.saveTag(blog.getId(), tag.getId());
            }
        } else {
            result = blogService.updateBlog(blog.getId(), blog);
        }

        if (result != 1) {
            attributes.addFlashAttribute("message", "操作失败");
        } else {
            attributes.addFlashAttribute("message", "操作成功");
        }
        return "redirect:/admin/blogs";
    }

    // 编辑博客
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable int id, Model model, RedirectAttributes attributes) {
        // TODO:编辑的页面有问题，删除成功的信息还需要添加。
        model.addAttribute("types", typeService.getAllType());
        model.addAttribute("tags", tagService.getAllTag());

        Blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog", blog);

        return "/admin/blog-input";
    }

    // 删除博客
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable int id, RedirectAttributes attributes) {
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/blogs";
    }
}
